/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.ui.field;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.daikit.daikit4gxt.client.ui.component.DkSpacer;
import com.daikit.daikit4gxt.client.ui.forms.AbstractDkHideableAdapterField;
import com.daikit.daikit4gxtsample.shared.bean.LocaleData;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.ui.InlineLabel;
import com.sencha.gxt.widget.core.client.Collapsible;
import com.sencha.gxt.widget.core.client.ComponentHelper;
import com.sencha.gxt.widget.core.client.button.ToolButton;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.BeforeCollapseEvent;
import com.sencha.gxt.widget.core.client.event.CollapseEvent;
import com.sencha.gxt.widget.core.client.event.ExpandEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.TextField;


/**
 * Locale Editor UI
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class LocaleEditor extends AbstractDkHideableAdapterField<Map<LocaleData, String>> implements Collapsible
{
	private final Map<LocaleData, TextField> textFields = new HashMap<LocaleData, TextField>();

	private boolean collapsed;
	private ToolButton collapseButton;
	private final int labelWidth = 40;
	private final int spacerWidth = 10;

	/**
	 * Constructor
	 * 
	 * @param allPossibleLocales
	 */
	public LocaleEditor(final List<LocaleData> allPossibleLocales)
	{
		super(null);

		//		final HorizontalLayoutContainer widget = new HorizontalLayoutContainer() ;
		final VerticalLayoutContainer verticalPanel = new VerticalLayoutContainer();

		for (final LocaleData locale : allPossibleLocales)
		{
			final HBoxLayoutContainer line = new HBoxLayoutContainer();
			line.getElement().getStyle().setMarginBottom(2, Unit.PX);
			final InlineLabel label = new InlineLabel(locale.getLabel());
			label.setWidth(40 + "px");
			final TextField textField = new TextField();
			textFields.put(locale, textField);
			line.add(label);
			line.add(new DkSpacer(spacerWidth));
			line.add(textField);
			verticalPanel.add(line, new VerticalLayoutData(1, 22));
		}
		// create the collapse button
		//		getCollapseButton();
		setWidget(verticalPanel);

		verticalPanel.addResizeHandler(new ResizeHandler()
		{

			@Override
			public void onResize(final ResizeEvent event)
			{
				final int width = event.getWidth();
				if (width > 0)
				{
					final int textFieldWidth = width - labelWidth - spacerWidth;
					for (final TextField widget : textFields.values())
					{
						widget.setWidth(textFieldWidth);
					}
				}
			}
		});
	}

	@Override
	public void setEnabled(final boolean enabled)
	{
		for (final TextField textField : textFields.values())
		{
			textField.setEnabled(enabled);
		}
	}

	@Override
	public void setValue(final Map<LocaleData, String> value)
	{
		for (final Entry<LocaleData, TextField> entry : textFields.entrySet())
		{
			entry.getValue().setValue(value.get(entry.getKey()));
		}
	}

	@Override
	public Map<LocaleData, String> getValue()
	{
		final Map<LocaleData, String> value = new HashMap<LocaleData, String>();
		for (final Entry<LocaleData, TextField> entry : textFields.entrySet())
		{
			value.put(entry.getKey(), entry.getValue().getValue());
		}
		return value;
	}

	/**
	 * @return the collapse {@link ToolButton}
	 */
	public ToolButton getCollapseButton()
	{
		if (collapseButton == null)
		{
			collapseButton = new ToolButton(ToolButton.UP);
			collapseButton.addSelectHandler(new SelectHandler()
			{
				@Override
				public void onSelect(final SelectEvent event)
				{
					setExpanded(!isExpanded());
				}
			});
			if (isAttached())
			{
				ComponentHelper.doAttach(collapseButton);
			}
		}
		return collapseButton;
	}

	@Override
	public void collapse()
	{
		if (fireCancellableEvent(new BeforeCollapseEvent()))
		{
			this.collapsed = true;
			getCollapseButton().changeStyle(ToolButton.DOWN);
			fireEvent(new CollapseEvent());
		}
	}

	@Override
	public void expand()
	{
		if (fireCancellableEvent(new BeforeCollapseEvent()))
		{
			this.collapsed = false;
			getCollapseButton().changeStyle(ToolButton.UP);
			fireEvent(new ExpandEvent());
		}
	}

	@Override
	public boolean isExpanded()
	{
		return !collapsed;
	}

	/**
	 * Convenience method to expand / collapse the field set by invoking {@link #expand()} or {@link #collapse()}.
	 * 
	 * @param expand
	 *           true to expand the field set, otherwise collapse
	 */
	public void setExpanded(final boolean expand)
	{
		if (expand)
		{
			expand();
		}
		else
		{
			collapse();
		}
	}

}
